package com.wwwarehouse.carddesk.fragment.functionlogincontrol;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskGroupresponseBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskFunctionResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.custom_widget.textinput.TextInputLayout;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * 团队框架 ---- 暂停开启登录
 * minifly
 * 2017:11:15 9:30
 *
 * @author minfily
 */
@Route(path = CardDeskConstant.PATH_GROUP_STOPLOGIN )
public class FunctionLoginControlFragment extends BaseFragment {

    private View mRootView;
    private ClearEditText mGroupLoginControlEdittext;
    private TextInputLayout mGroupLoginControlInputlayout;
    private RelativeLayout emptyRel;
    private StateButton searchBtn;
    public static final int REQUEST_ALLPEOPLE = 10004,REQUEST_ALLPEOPLE_STOP = 10005;
    private int pageSize = 7, total = 0;
    private String buId;
    private String name;
    private CardDeskFunctionResponseBean.TaskBean bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_group_login_control, container, false);
        initView(mRootView);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }


    @Override
    public void setTitle() {
        if (peekFragment() instanceof FunctionLoginControlFragment) {
            mActivity.setTitle(getString(R.string.teamdesk_login_control));
        }
    }

    @Override
    public void requestDatas() {

    }

    private void initView(View mRootView) {
        mGroupLoginControlEdittext = (ClearEditText) mRootView.findViewById(R.id.group_login_control_edittext);
        mGroupLoginControlInputlayout = (TextInputLayout) mRootView.findViewById(R.id.group_login_control_inputlayout);
        searchBtn = (StateButton) mRootView.findViewById(R.id.group_login_control_statebutton);
        emptyRel = (RelativeLayout) mRootView.findViewById(R.id.group_login_control_empty_rel);

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAllPeople();
            }
        });

        bean = (CardDeskFunctionResponseBean.TaskBean) getArguments().getSerializable(Constant.KEY_CARD_DESK_FUNCTION_DETAILS);
        if(bean == null ){
            buId = getArguments().getString("buId");
        }else{
            buId = bean.getBusinessId();
        }

        EventBus.getDefault().register(this);


        searchBtn.setEnabled(false);
        mGroupLoginControlEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String key = mGroupLoginControlEdittext.getText().toString().trim();
                if(StringUtils.isNullString(key)){
                    searchBtn.setEnabled(false);
                }else{
                    searchBtn.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    public void getAllPeople() {

        name = mGroupLoginControlEdittext.getText().toString().trim();

        Map<String, Object> map = new HashMap<>();
        /**
         * buId	业务单位	number	@mock=260001
         name	名字	string	@mock=
         page	页	number
         size	一页大小	number
         */
        map.put("buId", buId);
        map.put("name", "" + name);
        map.put("page", 1);
        map.put("size", pageSize);
        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_STOPLOGINGETALLPEPLE, map, myOnresponse, REQUEST_ALLPEOPLE);
        showProgressDialog();
    }


    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_ALLPEOPLE:
                    if("0".equals(commonClass.getCode())){
                        CardDeskGroupresponseBean bean = JSON.parseObject(commonClass.getData().toString(), CardDeskGroupresponseBean.class);
                        total = bean.getTotal();
                        if (bean.getList().size() == 0) {
                            emptyRel.setVisibility(View.VISIBLE);
                        } else {
                            mGroupLoginControlEdittext.setText("");
                            emptyRel.setVisibility(View.GONE);
                            Bundle bundle = new Bundle();
                            bundle.putInt("total" , total);
                            bundle.putParcelable("bean" , bean);
                            bundle.putString("buId" , buId);
                            bundle.putString("name" , name);

                            FunctionLoginControlSearchresultPagerFragment fragment = new FunctionLoginControlSearchresultPagerFragment();
                            fragment.setArguments(bundle);
                            pushFragment(fragment);
                        }
                    }else{
                        toast("" + commonClass.getMsg());
                    }
                    break;
                default:

                    break;
            }

        }

        @Override
        public void onFailed(String result, int what) {

        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    public void onEventMainThread(BackListenerEvent event) {
        String message = event.getMsg();
        if (message.equals("FunctionLoginControlFragment")) {
            if(emptyRel.getVisibility() == View.VISIBLE){
                emptyRel.setVisibility(View.GONE);
                return;
            }else{
                popFragment();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
